package com.yuexunit.finance.common.httpclient;

import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*@version 创建时间：2017年1月18日 上午10:52:28
*@author wenwenlong
*类说明
*/

public class ServiceClientHelper {
	private static Logger logger = LoggerFactory.getLogger(ServiceClientHelper.class);
	public static String invoke(String url, String method, Map<String, String> params) {
		RPCServiceClient serviceClient;
		try {
			serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			EndpointReference targetEPR = new EndpointReference(url);
			
			options.setTo(targetEPR);
			options.setTimeOutInMilliSeconds(600000L);
			QName opAddentry = new QName("http://service.axis2Service.ws.rj.com", method);
			StringBuilder sb = new StringBuilder();
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			sb.append("<XmlReq>\n");
			for(Map.Entry<String, String> entry: params.entrySet()) {
				sb.append("<"+ entry.getKey() +">" + entry.getValue() + "</"+ entry.getKey() +">");
			}
			sb.append("</XmlReq>");
			String xmlString = sb.toString();
			Object[] opAddEntryArgs = new Object[] {xmlString};
			Class[] classes = new Class[] {String.class};
			System.out.println("xmlString:" + xmlString);
			logger.info("url:" + url);
			logger.info("method:" + method);
			logger.info("xmlString:" + xmlString);
			Object[] result = serviceClient.invokeBlocking(opAddentry, opAddEntryArgs, classes);
			System.out.println(result[0].toString());
			logger.info("xmlString:" + xmlString);
			logger.info("result:" + result[0].toString());
			return result[0].toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
